//===============================================================================
// Microsoft Architecture Strategy Team
// LitwareHR - SaaS Sample Application
//===============================================================================
// Copyright  Microsoft Corporation.  All rights reserved.
// THIS CODE AND INFORMATION IS PROVIDED "AS IS" WITHOUT WARRANTY
// OF ANY KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING BUT NOT
// LIMITED TO THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
// FITNESS FOR A PARTICULAR PURPOSE.
//===============================================================================
// The example companies, organizations, products, domain names,
// e-mail addresses, logos, people, places, and events depicted
// herein are fictitious.  No association with any real company,
// organization, product, domain name, email address, logo, person,
// places, or events is intended or should be inferred.
//===============================================================================


using System;
using System.Collections.Generic;
using System.Text;
using System.Collections.ObjectModel;

namespace ConfigurationWizard.Setup
{
    public class Task
    {
        string description;
        string scriptLocation;
        private Collection<OSBuild> osBuilds = null;        
        bool displayConsole = false;
        string verb = string.Empty;

        public Task(string description, string scriptLocation, bool displayConsole, string verb)
        {
            this.description = description;
            this.scriptLocation = scriptLocation;
            this.displayConsole = displayConsole;
            this.verb = verb;
            
        }

        /// <summary>
        /// Gets the task description
        /// </summary>
        public string Description
        {
            get { return description; }
        }

        /// <summary>
        /// Gets the task script location
        /// </summary>
        public string ScriptLocation
        {
            get { return scriptLocation; }
        }

        /// <summary>
        /// If exists express the valid build numbers for this task to be run
        /// </summary>
        public Collection<OSBuild> OSBuilds
        {
            get { return osBuilds; }
            set { osBuilds = value; }
        }

        /// <summary>
        /// Indicates if the task will be shown in a console window
        /// </summary>
        public bool DisplayConsole
        {
            get { return displayConsole; }
        }

        public string Verb
        {
            get { return verb;  }
        }

        public bool Execute()
        {
            System.Diagnostics.Process process = new System.Diagnostics.Process();
            process.StartInfo = new System.Diagnostics.ProcessStartInfo(this.scriptLocation);            
            process.StartInfo.RedirectStandardOutput = !displayConsole;
            process.StartInfo.RedirectStandardInput = !displayConsole;
            process.StartInfo.RedirectStandardError = !displayConsole;
            process.StartInfo.UseShellExecute = false;
            if( verb != string.Empty )
                process.StartInfo.Verb = Verb;
            process.StartInfo.CreateNoWindow = !displayConsole;
            process.Start();
            process.WaitForExit();
            return process.ExitCode == 0;
        }

        internal bool IsEnableForThisOS(string buildNumber)
        {
            bool found = false;
            if (this.OSBuilds.Count == 0)
            {
                found = true;
            }
            else
            {
                foreach (OSBuild osb in this.OSBuilds)
                {
                    if (0 == String.Compare(osb.Number, buildNumber, StringComparison.InvariantCulture))
                    {
                        found = true;
                        break;
                    }
                }
            }
            return found;
        }
    }
}
